//
//

#import "YMActionView.h"
#import <Masonry/Masonry.h>

#define kTradePWD_LENGTH_LIMIT 4
@interface YMActionView()<UITextFieldDelegate>

@property (nonatomic,strong)UIView *bgView;
@property (nonatomic,strong)UIButton *sureBtn;
@property (nonatomic,strong)UIButton *cancleBtn;
@property (nonatomic,strong)UIView *textView;
@property (nonatomic,strong)UIView *liveView;
@property (nonatomic,strong)UIView *liveViewT;
@property (nonatomic,strong)UIButton *coverBtn;


@end
@implementation YMActionView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.frame = [UIScreen mainScreen].bounds;
        [self initView];
        self.nameTextField.text = @"";
        [self.coverBtn addTarget:self action:@selector(cancleAct) forControlEvents:UIControlEventTouchUpInside];

    }
    
    return self;
}
-(void)initView{
    
    [self addSubview:self.coverBtn];
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.mas_left).offset(50);
        make.right.mas_equalTo(self.mas_right).offset(-50);
        make.height.mas_equalTo(175);
        make.top.mas_equalTo(self.mas_top).offset([UIScreen mainScreen].bounds.size.height/2-100);
        
    }];
    [self.bgView addSubview:self.titleLable];
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.bgView.mas_centerX);
        make.top.mas_equalTo(self.bgView.mas_top).offset(20);
        
    }];
    
    [self.bgView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.titleLable.mas_bottom).offset(21);
        make.left.mas_equalTo(self.bgView.mas_left).offset(27);
        make.right.mas_equalTo(self.bgView.mas_right).offset(-27);
        make.height.mas_equalTo(36);
        
    }];
    
    [self.textView addSubview:self.nameTextField];
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.textView.mas_centerY);
        make.left.mas_equalTo(self.textView.mas_left).offset(16);
        make.height.mas_equalTo(33);
        make.right.mas_equalTo(self.textView.mas_right).offset(-15);
        
    }];
    
    [self.bgView addSubview:self.liveView];
    [self.liveView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.bgView.mas_left);
        make.right.mas_equalTo(self.bgView.mas_right);
        make.height.mas_equalTo(0.17);
        make.top.mas_equalTo(self.textView.mas_bottom).offset(30);
        
    }];
    
    [self.bgView addSubview:self.liveViewT];
    [self.liveViewT mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.width.mas_equalTo(0.35);
        make.top.mas_equalTo(self.liveView.mas_bottom);
        make.bottom.mas_equalTo(self.bgView.mas_bottom);
        make.centerX.mas_equalTo(self.bgView.mas_centerX);
            
    }];
    
    [self.bgView addSubview:self.sureBtn];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.liveViewT.mas_right);
        make.centerY.mas_equalTo(self.liveViewT.mas_centerY);
        make.width.mas_equalTo(([UIScreen mainScreen].bounds.size.width-100)/2);
        make.height.mas_equalTo(60);
 
            
    }];
   
    [self.bgView addSubview:self.cancleBtn];
    [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(self.liveViewT.mas_left);
        make.centerY.mas_equalTo(self.liveViewT.mas_centerY);
        make.width.mas_equalTo(self.sureBtn.mas_width);
        make.height.mas_equalTo(self.sureBtn.mas_height);
        
    }];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:0.3f];
    float width = self.bgView.frame.size.width;
    float height = self.bgView.frame.size.height;
    CGRect rect = CGRectMake(50,200,width,height);
    self.bgView.frame = rect;
    [UIView commitAnimations];
    
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    //滑动效果
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.bgView.frame.size.width;
    float height = self.bgView.frame.size.height;
    CGRect rect = CGRectMake(50,([UIScreen mainScreen].bounds.size.height-250)/2,width,height);
    //恢复屏幕
    self.bgView.frame =  rect;
    [UIView commitAnimations];
}


-(void)sureAct{

    self.textBlock(self.nameTextField.text);

}
//取消弹窗
-(void)cancleAct{
    
    self.nameTextField.text = @"";
    if (self->_isShow) {
        
        [UIView animateWithDuration:0.3 animations:^{
            self.coverBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
            self.bgView.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            self->_isShow = NO;
        }];
    }

}

#pragma mark 显示alertview
-(void)show{
    
    if (self->_isShow) return;
    
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    [rootVC.view addSubview:self];
    self.bgView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.coverBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        self.bgView.alpha = 1;
    } completion:^(BOOL finished) {
        self->_isShow = YES;
    }];

}
- (UIButton *)coverBtn {
    if (!_coverBtn) {
        _coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _coverBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        _coverBtn.frame = self.bounds;
    }
    return _coverBtn;
}
-(UIView *)liveViewT{
    
    if (_liveViewT == nil) {
        
        _liveViewT = [[UIView alloc]init];
        _liveViewT.backgroundColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:0.3];
        
    }
    
    return _liveViewT;
}
-(UIView *)liveView{
    
    if (_liveView == nil) {
        
        _liveView = [[UIView alloc]init];
        _liveView.backgroundColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:0.5];
        
    }
    
    return _liveView;
}
-(UITextField *)nameTextField{
    
    if (_nameTextField == nil) {
        
        _nameTextField = [[UITextField alloc]init];
        _nameTextField.font = [UIFont systemFontOfSize:16];
        _nameTextField.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        _nameTextField.delegate = self;
        _nameTextField.text = @"";
        _nameTextField.keyboardType = UIKeyboardTypeNumberPad;
        
    }
    
    return _nameTextField;
}
-(UIView *)textView{
    
    if (_textView == nil) {
        
        _textView = [[UIView alloc]init];
        _textView.layer.borderColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:0.6].CGColor;
        _textView.layer.borderWidth = 0.5;
        _textView.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        _textView.layer.cornerRadius = 4;
    }
    
    return _textView;
}
-(UIButton *)cancleBtn{
    
    if (_cancleBtn == nil) {
        
        _cancleBtn = [[UIButton alloc]init];
        [_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancleBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0] forState:UIControlStateNormal];
        _cancleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_cancleBtn addTarget:self action:@selector(cancleAct) forControlEvents:UIControlEventTouchUpInside];
        _cancleBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    
    return _cancleBtn;
}
-(UIButton *)sureBtn{
    
    if (_sureBtn == nil) {
        
        _sureBtn = [[UIButton alloc]init];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor colorWithRed:51/255.0 green:133/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_sureBtn addTarget:self action:@selector(sureAct) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    
    return _sureBtn;
}
-(UILabel *)titleLable{
    
    if (_titleLable == nil) {
        
        _titleLable = [[UILabel alloc]init];
        _titleLable.font =[UIFont systemFontOfSize:16];
        _titleLable.textColor = [UIColor grayColor];
    
    }
    
    return _titleLable;
}

-(UIView *)bgView{
    
    if (_bgView == nil) {
        
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 10;
        _bgView.clipsToBounds = YES;
        
    }
    
    return _bgView;
}
@end
